<?php

namespace app\admin\controller;

use think\Controller;

use think\Db;
use think\Session;
use think\Request;
use app\admin\model\GoodsModel;
use app\admin\model\LabelModel;

//产品管理
class Goods extends Common
{   
  //栏目添加
  public function add()
  {     
        if(Request::instance()->isPost()){
          $post=input('post.');
          // dump($post);
          // return;
          $da=GoodsModel::where('name',$post['name'])->find();
          if($da){
            return ['code'=>0,'msg'=>'该栏目已存在','data'=>''];
          }

          $goods=new GoodsModel($post);
          $num=$goods->allowField(true)->save();
          if($num>0){
            return ['code'=>1,'msg'=>'添加成功','data'=>''];
          }else{
            return ['code'=>0,'msg'=>'添加失败','data'=>''];
          }
        }
        $get=input('get.');
        if(isset($get['cid'])){
          $this->assign('cid',$get['cid']);
        }else{
          $this->assign('cid','');
        }
        $label=LabelModel::order('id desc')->select();
        $this->assign('label',$label);
        return $this->fetch();
  }
  //栏目修改
  public function edit()
  {     
        $id=input('id');
        if(Request::instance()->isPost()){
          $post=input('post.');
          $da=GoodsModel::where('name',$post['name'])->where('id','<>',$id)->find();
          if($da){
            return ['code'=>0,'msg'=>'该栏目已存在','data'=>''];
          }

          $goods=new GoodsModel;
          $num=$goods->allowField(true)->save($post,['id'=>$id]);
          if($num>0){
            return ['code'=>1,'msg'=>'修改成功','data'=>''];
          }else{
            return ['code'=>0,'msg'=>'修改失败','data'=>''];
          }
        }else{
          $data=GoodsModel::get($id);
          $this->assign('data',$data);
          $label=LabelModel::order('id desc')->select();
          $this->assign('label',$label);
          return $this->fetch();
        }
  }
  //栏目列表
  public function index()
  {   
        $get=input('get.');
        if(isset($get['cid'])){
          $this->assign('cid',$get['cid']);
        }else{
          $this->assign('cid','');
        }
        $label=LabelModel::order('id desc')->select();
        $this->assign('label',$label);
      return $this->fetch();
  }
  //栏目数据
  public function lst()
  { 
        $req=input("post.");
        $get=input("get.");

        $page=$req['page'];  //当前页   1

        $limit=$req['limit']; //每页几行数据  10

        $begin=($page-1)*$limit;   //0,10  10,10 
        $where = [];
        if (isset($req['time']) ) {
            if (!empty($req['time'])){
                $arr = explode('~',$req['time']);
                $arr[0] = strtotime($arr[0]);
                $arr[1] = strtotime($arr[1])+24*3600;
                $where['create_time'] = ['between',$arr];
            }
        }
        if (isset($req['name']) ) {
            if (!empty($req['name'])){
                $where['name']=['like','%'.$req['name'].'%'];
            }
        }
        if (isset($get['cid']) && $get['cid']) {
            if (!empty($get['cid'])){
                $where['cid']=$get['cid'];
            }
        }
        if (isset($req['cid']) && $req['cid']) {
            if (!empty($req['cid'])){
                $where['cid']=$req['cid'];
            }
        }
        $msg['code']  = 0;
        $msg['msg']   = '返回成功';
        $msg['count'] = GoodsModel::where($where)->count();
        $list         = GoodsModel::where($where)
                                    ->order('sort asc')
                                    ->limit(input('limit'))
                                    ->page(input('page'))
                                    ->select();
        $msg['data'] = $list;
        echo json_encode($msg,320);
  }
  //执行栏目删除
  public function remove()
  { 
      $post=input("post.");
      $num=GoodsModel::destroy($post['id']);
      if($num){
          return ['code'=>1,'msg'=>'删除成功','data'=>''];
      }else{
        return ['code'=>0,'msg'=>'删除失败','data'=>''];
      }
  }
   //修改状态
    public function up_status()
    { 
       $id=input("get.id");
       $data=Db::table('goods')->where('id',$id)->find();
       $status=$data['status'];
       if($status==1){  //当前启用
          $num=Db::table('goods')->where('id',$id)->update(['status'=>2]);
          if($num>0){
               return 1;
          }else{
               return 2;
          }
      }else{
          $num=Db::table('goods')->where('id',$id)->update(['status'=>1]);
          if($num>0){
               return 3;
          }else{
               return 4;
          }
       }
    }

  //文件上传
    public function upload(){
      $file = request()->file('file');
      $path='goods';
      return json_decode($this->uploads($file,$path),true);
    }
}



